********************************************************************************
* Do External Threats Reduce Affective Polarization? An Experiment on Russia's Invasion of Ukraine 
* MAIN PAPER
* BY JONAS PILGAARD KAISER & MARKUS SEIER
********************************************************************************

*Replicators should set directory to open data
global path "your_path_here"
cd "$path"

*Start logging
log using "Replication_Main_Log.log", text replace

*Creates folders
cap mkdir "Figures"
cap mkdir "Tables"

*Clear all existing data
clear all

*Load data
use "Data_Clean.dta"

*Define controls 
glo controls1 "Age Male Ethnicitydum1 Ethnicitydum2 Ethnicitydum3 Ethnicitydum4 Schoolingdum1 Schoolingdum3 Schoolingdum4 Schoolingdum5 Schoolingdum6 Schoolingdum7 Employed"
glo controls2 "Age Male Ethnicitydum1 Ethnicitydum2 Ethnicitydum3 Ethnicitydum4 Schoolingdum1 Schoolingdum3 Schoolingdum4 Schoolingdum5 Schoolingdum6 Schoolingdum7 Employed Political_Interest StrongSupporter Democrat"
glo controls1_logit "Age i.Male i.Ethnicitydum1 i.Ethnicitydum2 i.Ethnicitydum3 i.Ethnicitydum4 i.Schoolingdum1 i.Schoolingdum3 i.Schoolingdum4 i.Schoolingdum5 i.Schoolingdum6 i.Schoolingdum7 i.Employed"
glo controls2_logit "Age i.Male i.Ethnicitydum1 i.Ethnicitydum2 i.Ethnicitydum3 i.Ethnicitydum4 i.Schoolingdum1 i.Schoolingdum3 i.Schoolingdum4 i.Schoolingdum5 i.Schoolingdum6 i.Schoolingdum7 i.Employed Political_Interest i.StrongSupporter i.Democrat"

********************************************************************************
*** FIGURE 2
********************************************************************************

*Generate and save first plot
vioplot FT_InParty, over(Democrat) xlabel(1 "Republicans" 2 "Democrats", nogrid) ytitle(Feeling Thermometer) xscale(noline) density(color(gs8)) bar(color(black)) line(color(black)) title("Own Party") ylabel(0(20)100)
graph save figures/temp1.gph, replace
*Generate and save second plot 
vioplot FT_OutParty, over(Democrat) xlabel(1 "Republicans" 2 "Democrats", nogrid) ytitle("") xscale(noline) density(color(gs8)) bar(color(black)) line(color(black)) title("Opposite Party") yscale(off) 
graph save figures/temp2.gph, replace
*Combine plots
graph combine figures/temp1.gph figures/temp2.gph, ycommon xcommon imargin(0 0) note() 
*Export figure
graph export figures/fig2.eps, replace

********************************************************************************
*** TABLE 2
********************************************************************************

*Estimate and store column 1 regression
reg AP_FT Invasion_Condition if Disagreement_Condition==0, robust
eststo AP_FT1_H1
*Estimate and store column 2 regression
reg AP_FT Invasion_Condition $controls1 if Disagreement_Condition==0, robust
eststo AP_FT2_H1
*Estimate and store column 3 regression
reg AP_FT Invasion_Condition $controls2 if Disagreement_Condition==0, robust
eststo AP_FT3_H1
*Estimate and store column 4 regression
reg AP_FT Invasion_Condition if Control_Condition==0, robust
eststo AP_FT1_H3
*Estimate and store column 5 regression
reg AP_FT Invasion_Condition $controls1 if Control_Condition==0, robust
eststo AP_FT2_H3
*Estimate and store column 6 regression
reg AP_FT Invasion_Condition $controls2 if Control_Condition==0, robust
eststo AP_FT3_H3

*Combine regression outputs and export
esttab AP_FT1_H1 AP_FT2_H1 AP_FT3_H1 AP_FT1_H3 AP_FT2_H3 AP_FT3_H3 using Tables/Table2.tex, replace b(%12.3f) se(%12.3f) nomtitles starlevels(* 0.10 ** 0.05 *** 0.01) stats(N r2_a, fmt(0 2) labels(N "Adj. R2")) title("Regressions of difference in own/opposite-party Feeling Thermometer rating on Treatment and controls.") order(Invasion_Condition Disagreement_Condition Age Male Ethnicitydum1 Ethnicitydum2 Ethnicitydum3 Ethnicitydum4 Schoolingdum5 Schoolingdum4 Schoolingdum7 Schoolingdum1 Schoolingdum4 Schoolingdum3) varlabels(_cons Constant Invasion_Condition "Invasion" Disagreement_Condition "Disagreement" Ethnicitydum1 "Asian American" Ethnicitydum2 "African American" Ethnicitydum3 "Hispanic" Ethnicitydum4 "Other Ethnicity" Schoolingdum1 "Associate Degree" Schoolingdum3 "Doctorate Degree" Schoolingdum4 "HS Degree" Schoolingdum5 "Less than HS Degree" Schoolingdum6 "Master's Degree" Schoolingdum7 "Some College" Political_Interest "Political Interest" StrongSupporter "Strong Supporter")  

*Clear stored estimates 
eststo clear

********************************************************************************
*** FIGURE 3
********************************************************************************

*Generate temp. variables for plot
gen run=_n
gen pp=. 
replace pp=1 if run<350
replace pp=3 if run>=350 & run<700
replace pp=5 if run>=700 & run<1050
replace pp=7 if run>=1050

*Manually generate numbers for the bars
gen share1=.
gen share1_hi=.
gen share1_li=.
sum P1_Type2 if AP_FT<26
replace share1=.0571429*100 if pp==1 
replace share1_hi=share1+invttail(350-1,0.025)*(0.2324477/sqrt(350))*100 if pp==1
replace share1_li=share1-invttail(350-1,0.025)*(0.2324477/sqrt(350))*100 if pp==1
gen share2=.
gen share2_hi=.
gen share2_li=.
sum P1_Type2 if AP_FT>=26 & AP_FT<50
replace share2=.0574713*100 if pp==3 
replace share2_hi=share2+invttail(261-1,0.025)*(0.233188/sqrt(261))*100 if pp==3
replace share2_li=share2-invttail(261-1,0.025)*(0.233188/sqrt(261))*100 if pp==3
gen share3=.
gen share3_hi=.
gen share3_li=.
sum P1_Type2 if AP_FT>=50 & AP_FT<70
replace share3=.1276042*100 if pp==5 
replace share3_hi=share3+invttail(384-1,0.025)*(0.3340838/sqrt(384))*100 if pp==5
replace share3_li=share3-invttail(384-1,0.025)*(0.3340838/sqrt(384))*100 if pp==5
gen share4=.
gen share4_hi=.
gen share4_li=.
sum P1_Type2 if AP_FT>=70
replace share4=.1911765*100 if pp==7
replace share4_hi=share4+invttail(417-1,0.025)*(0.3937105/sqrt(417))*100 if pp==7
replace share4_li=share4-invttail(417-1,0.025)*(0.3937105/sqrt(417))*100 if pp==7	

*Produce and save the graph
graph twoway (bar share1 pp, color(red*1.2)) (rcap share1_hi share1_li pp, color(black)) (bar share2 pp, color(red*0.8)) (rcap share2_hi share2_li pp, color(black)) (bar share3 pp, color(blue*0.8)) (rcap share3_hi share3_li pp, color(black)) (bar share4 pp, color(blue*1.2)) (rcap share4_hi share4_li pp, color(black)), note("") title("") xlabel(1 "1st Quartile FT" 3 "2nd Quartile FT" 5 "3rd Quartile FT" 7 "4th Quartile FT", nogrid) xscale(noline) xtitle("") ytitle("Affective Polarization in aBoS: Type BA (%)") ylabel(0(5)30) yscale(range(0 30)) legend(off)
graph export figures/fig3.eps, replace 

*Drop temp. variables 
drop run pp share1 share1_hi share1_li share2 share2_hi share2_li share3 share3_hi share3_li share4 share4_hi share4_li

********************************************************************************
*** FIGURE 4
********************************************************************************

*Generate temp. variables for plot
gen run=_n
gen tt=. 
replace tt=1 if run<200
replace tt=2 if run>=200 & run<400
replace tt=3 if run>=400 & run<600
replace tt=5 if run>=600 & run<800
replace tt=6 if run>=800 & run<1000
replace tt=7 if run>=1000

*Manually generate numbers for the bars
gen dec_inv=.
gen hi_inv=.
gen li_inv=. 
replace dec_inv=0.495614*100 if tt==1
replace hi_inv=dec_inv+invttail(456-1,0.025)*(0.5005299/sqrt(456))*100 if tt==1
replace li_inv=dec_inv-invttail(456-1,0.025)*(0.5005299/sqrt(456))*100 if tt==1
replace dec_inv=0.4210526*100 if tt==5
replace hi_inv=dec_inv+invttail(456-1,0.025)*(0.4942702/sqrt(456))*100 if tt==5
replace li_inv=dec_inv-invttail(456-1,0.025)*(0.4942702/sqrt(456))*100 if tt==5
gen dec_con=. 
gen hi_con=.
gen li_con=. 
replace dec_con=0.4425532*100 if tt==2
replace hi_con=dec_con+invttail(470-1,0.025)*(0.4972181/sqrt(470))*100 if tt==2
replace li_con=dec_con-invttail(470-1,0.025)*(0.4972181/sqrt(470))*100 if tt==2
replace dec_con=0.3617021*100 if tt==6
replace hi_con=dec_con+invttail(470-1,0.025)*(0.4810052/sqrt(470))*100 if tt==6
replace li_con=dec_con-invttail(470-1,0.025)*(0.4810052/sqrt(470))*100 if tt==6
gen dec_dis=.
gen hi_dis=.
gen li_dis=.
replace dec_dis=0.475891*100 if tt==3
replace hi_dis=dec_dis+invttail(477-1,0.025)*(0.4999427/sqrt(477))*100 if tt==3
replace li_dis=dec_dis-invttail(477-1,0.025)*(0.4999427/sqrt(477))*100 if tt==3
replace dec_dis=0.3962264*100 if tt==7
replace hi_dis=dec_dis+invttail(477-1,0.025)*(0.489626/sqrt(477))*100 if tt==7
replace li_dis=dec_dis-invttail(477-1,0.025)*(0.489626/sqrt(477))*100 if tt==7

*Produce and save the graph
graph twoway (bar dec_inv tt, color(red*1.2)) (rcap hi_inv li_inv tt, color(black)) (bar dec_con tt, color(gs8)) (rcap hi_con li_con tt, color(black)) (bar dec_dis tt, color(sand)) (rcap hi_dis li_dis tt, color(black)), title("") xlabel(2 "Own-Party Condition" 6 "Opposite-Party Condition", nogrid labsize(medlarge)) xscale(noline) xtitle("") ytitle("Choosing B (%)", size(large)) ylabel(0(10)70) yscale(range(0 70)) ylabel(0(10)70, labsize(medlarge)) legend(order(1 "Invasion" 3 "Control" 5 "Disagreement") size(medlarge)) 
graph export figures/fig4.eps, replace 

*Drop temp. variables 
drop run tt dec_inv hi_inv li_inv dec_con hi_con li_con dec_dis hi_dis li_dis 

********************************************************************************
*** ALL ADDITIONAL RESULTS IN THE PAPER 
********************************************************************************

*Hedges g (affective polarization in feeling thermometers)
esize twosample AP_FT if Disagreement_Condition==0, by(Invasion_Condition) all
esize twosample AP_FT if Invasion_Condition==0, by(Disagreement_Condition) all
esize twosample AP_FT if Control_Condition==0, by(Invasion_Condition) all

*aBoS descriptive statistics: 
tab P1_InParty, freq
tab P1_OutParty, freq
tab P1_InParty P1_OutParty, col
mcci 690 52 162 499	//McNemar 

tab P1_Type

*Multinomial logit to estimate relation between behavior as Player 1 in aBoS and FT difference 
mlogit P1_Type AP_FT, base(1) robust
margins, dydx(AP_FT) atmeans post
mlogit P1_Type AP_FT $controls1_logit, base(1) robust
margins, dydx(AP_FT) atmeans post
mlogit P1_Type AP_FT $controls2_logit, base(1) robust
margins, dydx(AP_FT) atmeans post

*Logit regressions (treatment effects on Player 1 decision in aBoS
logit P1_InParty i.Invasion_Condition if Disagreement_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_InParty i.Invasion_Condition $controls1_logit if Disagreement_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_InParty i.Invasion_Condition $controls2_logit if Disagreement_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_OutParty i.Invasion_Condition if Disagreement_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_OutParty i.Invasion_Condition $controls1_logit if Disagreement_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_OutParty i.Invasion_Condition $controls2_logit if Disagreement_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_InParty i.Invasion_Condition if Control_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_InParty i.Invasion_Condition $controls1_logit if Control_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_InParty i.Invasion_Condition $controls2_logit if Control_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_OutParty i.Invasion_Condition if Control_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_OutParty i.Invasion_Condition $controls1_logit if Control_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

logit P1_OutParty i.Invasion_Condition $controls2_logit if Control_Condition==0 , robust
margins, dydx(Invasion_Condition) atmeans post

*Multinomial logit regression (treatment effects on Player 1 type in aBoS)
label define P1types 1 "BB" 2 "BA" 3 "AA" 4 "AB"
label values P1_Type P1types	//Label 'types'

mlogit P1_Type i.Invasion_Condition if Disagreement_Condition==0, base(1) robust
margins, dydx(i.Invasion_Condition) atmeans post

mlogit P1_Type i.Invasion_Condition $controls1_logit if Disagreement_Condition==0, base(1) robust
margins, dydx(i.Invasion_Condition) atmeans post

mlogit P1_Type i.Invasion_Condition $controls2_logit if Disagreement_Condition==0, base(1)	robust
margins, dydx(i.Invasion_Condition) atmeans post

mlogit P1_Type i.Invasion_Condition if Control_Condition==0, base(1) robust
margins, dydx(i.Invasion_Condition) atmeans post

mlogit P1_Type i.Invasion_Condition $controls1_logit if Control_Condition==0, base(1) robust
margins, dydx(i.Invasion_Condition) atmeans post

mlogit P1_Type i.Invasion_Condition $controls2_logit if Control_Condition==0, base(1) robust
margins, dydx(i.Invasion_Condition) atmeans post

*Close log
log close
